<html>
<head><meta charset="utf-8"><title>representing one of two traits · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/representing.20one.20of.20two.20traits.html">representing one of two traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="215790113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/representing%20one%20of%20two%20traits/near/215790113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/representing.20one.20of.20two.20traits.html#215790113">(Nov 06 2020 at 00:05)</a>:</h4>
<p>Is there a way to represent one of two traits in a trait bound? I have a <code>JoinSemilattice</code> trait and a <code>MeetSemilattice</code> trait, and I want to have an associated type that is one of the two. So I tried adding a <code>Semilattice</code> trait that is a supertrait of both, but that doesn't work the way I want it to.</p>
<p>Here's my trait definitions:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">Semilattice</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">op</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">other</span>: <span class="kp">&amp;</span><span class="nc">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">JoinSemilattice</span>: <span class="nc">Semilattice</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">op</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">other</span>: <span class="kp">&amp;</span><span class="nc">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">join</span><span class="p">(</span><span class="n">other</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">join</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">other</span>: <span class="kp">&amp;</span><span class="nc">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">MeetSemilattice</span>: <span class="nc">Semilattice</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">op</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">other</span>: <span class="kp">&amp;</span><span class="nc">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">meet</span><span class="p">(</span><span class="n">other</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">meet</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">other</span>: <span class="kp">&amp;</span><span class="nc">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>But then when I try to implement <code>MeetSemilattice</code>, I get this error:</p>
<div class="codehilite"><pre><span></span><code>error[E0277]: the trait bound `analyses::init_vars::InitVars: framework::domain::Semilattice` is not satisfied
  --&gt; src/analyses/init_vars.rs:20:6
   |
20 | impl MeetSemilattice for InitVars {}
   |      ^^^^^^^^^^^^^^^ the trait `framework::domain::Semilattice` is not implemented for `analyses::init_vars::InitVars`
   |
  ::: src/framework/domain.rs:21:28
   |
21 | pub trait MeetSemilattice: Semilattice {
   |                            ----------- required by this bound in `framework::domain::MeetSemilattice`
</code></pre></div>
<p>Is there any way to represent this in the trait system? Thanks so much!</p>



<a name="215790698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/representing%20one%20of%20two%20traits/near/215790698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/representing.20one.20of.20two.20traits.html#215790698">(Nov 06 2020 at 00:12)</a>:</h4>
<p>It looks there's been an RFC issue open for this since 2015: <a href="https://github.com/rust-lang/rfcs/issues/1024">rfc#1024</a></p>



<a name="215791026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/representing%20one%20of%20two%20traits/near/215791026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/representing.20one.20of.20two.20traits.html#215791026">(Nov 06 2020 at 00:17)</a>:</h4>
<p>It looks like rustc uses the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/dataflow/lattice/struct.Dual.html"><code>Dual</code></a> struct for this, but that seems like a suboptimal solution</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>